package com.dq.controller.op.opboss.message;

import com.dq.annotation.aop.SysLog;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.InstructionMessageQuery;
import com.dq.vo.Vo;
import com.dq.domain.message.oneself.InstructionMessage;
import com.dq.domain.message.oneself.centre.InstructionStaffuserCentre;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.service.AllService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cf
 */
@Api(description = "指令消息")
@RestController
@RequestMapping("sys/boss/instruction/")
public class BossInstructionMessageController extends AllService {

    @ApiOperation("boss获取创建的指令列表")
    @PostMapping("/list")
    @RequiresPermissions("sys:instruction:list")
    public Vo list() throws AdminException {
        InstructionMessageQuery instructionMessageQuery = new InstructionMessageQuery();
        instructionMessageQuery.setCreateUserId(sysUserService.getUser().getId());

        return instructionMessageService.getDisposeByBoss(instructionMessageQuery);
    }

    @ApiOperation("添加boss指令")
    @PostMapping("/save")
    @RequiresPermissions("sys:instruction:save")
    public Vo list(@ApiParam("userIds: 选择分配员工ID集合") @RequestBody InstructionMessage instructionMessage) throws AdminException {
        List<Long> userIds = instructionMessage.getUserIds();
        if(userIds == null || userIds.size() <= 0) {
            throw new AdminException("未选择分配员工");
        }

        // 保存指令
        instructionMessageService.save(instructionMessage);
        //保存指令分配员工
        for (Long userId : userIds) {
            InstructionStaffuserCentre instructionStaffuserCentre = new InstructionStaffuserCentre();
            SysUser sysUser = dao.get(SysUser.class, userId);
            instructionStaffuserCentre.setOrg(sysUser.getSection());
            instructionStaffuserCentre.setStaffUser(sysUser);
            instructionStaffuserCentre.setInstructionMessage(instructionMessage);

            instructionStaffuserCentreService.save(instructionStaffuserCentre);
        }

        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("删除boss指令")
    @SysLog("删除")
    @PostMapping("/delete/{id}")
    @RequiresPermissions("sys:instruction:delete")
    public Vo delete(@PathVariable @ApiParam(value = "id") Long id) throws AdminException {
        InstructionMessage orgPositionQuery = dao.get(InstructionMessage.class, id);
        if (orgPositionQuery == null) {
            throw new AdminException("无对应id对象");
        }

        orgPositionQuery.setDeleted(true);
        instructionMessageService.update(orgPositionQuery);
        return new Vo(HttpStatus.OK);
    }
}
